package com.ghy.seckill.mq;

import com.ghy.seckill.service.OmsOrderItemService;
import com.ghy.seckill.vo.OrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.annotation.RabbitListeners;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 创建秒杀订单
 */
@Component
@Slf4j
@RabbitListener(queues = "order.seckill.order.queue")
public class SeckillOrderListener {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private OmsOrderItemService orderItemService;

   @RabbitHandler
    public void addUserHit(OrderVo orderVo){
        System.out.println("订单服务创建订单："+orderVo.toString());
       Integer addOrder = orderItemService.addOrder(orderVo);
       log.info("创建订单成功");
       if (addOrder>0){
           //发送延时队列，一分钟后未支付，自动取消订单
           rabbitTemplate.convertAndSend("order-event-exchange","order.create.order",orderVo.getOrderSn());
       }


    }
}